﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;

namespace CPPEI.WPF.Core
{
    /// <summary>
    /// 窗口辅助类
    /// 1.处理窗口的全屏和非全屏显示
    /// </summary>
    public class XWindowHelper
    {
        private Window _targetWindow;
        private WindowState _windowState;
        private WindowStyle _windowStyle;
        private bool _windowTopMost;
        private ResizeMode _windowResizeMode;
        private Rect _windowRect;

        /// <summary>
        /// 是否是全屏模式
        /// </summary>
        public bool IsFullscreen { get; private set; }

        /// <summary>
        /// 构造窗口辅助类
        /// </summary>
        /// <param name="window">需要被处理的窗口对象</param>
        public XWindowHelper(Window window)
        {
            _targetWindow = window;

            _windowState = WindowState.Maximized;
            _windowStyle = WindowStyle.SingleBorderWindow;
            _windowTopMost = false;
            _windowResizeMode = ResizeMode.CanResize;

            if (window.WindowStyle == WindowStyle.None
                && window.WindowState == WindowState.Maximized)
            {
                IsFullscreen = true;
            }
            else
            {
                IsFullscreen = false;
            }
        }

        /// <summary>
        /// 进入全屏    
        /// </summary>
        public void EnterFullscreen()
        {
            //存储窗体信息       
            _windowState = _targetWindow.WindowState;
            _windowStyle = _targetWindow.WindowStyle;
            _windowTopMost = _targetWindow.Topmost;
            _windowResizeMode = _targetWindow.ResizeMode;
            _windowRect.X = _targetWindow.Left;
            _windowRect.Y = _targetWindow.Top;
            _windowRect.Width = _targetWindow.Width;
            _windowRect.Height = _targetWindow.Height;

            //变成无边窗体 
            _targetWindow.WindowState = WindowState.Normal;//假如已经是Maximized，就不能进入全屏，所以这里先调整状态 
            _targetWindow.WindowStyle = WindowStyle.None;
            _targetWindow.ResizeMode = ResizeMode.NoResize;

            _targetWindow.Topmost = true;//最大化后总是在最上面 
            _targetWindow.Topmost = false;//最大化后总是在最上面 

            //获取窗口句柄 
            var handle = new WindowInteropHelper(_targetWindow).Handle;

            _targetWindow.WindowState = WindowState.Maximized;
            IsFullscreen = true;
        }

        /// <summary>
        /// 退出全屏
        /// </summary>
        public void ExitFullscreen()
        {
            //恢复窗口先前信息，这样就退出了全屏 
            _targetWindow.Topmost = _windowTopMost;
            _targetWindow.WindowStyle = _windowStyle;
            _targetWindow.ResizeMode = ResizeMode.CanResize;//设置为可调整窗体大小 
            if (_windowRect.Width > 0)
            {
                _targetWindow.Left = _windowRect.Left;
                _targetWindow.Width = _windowRect.Width;
                _targetWindow.Top = _windowRect.Top;
                _targetWindow.Height = _windowRect.Height;
            }
            _targetWindow.WindowState = _windowState;//恢复窗口状态信息 
            _targetWindow.ResizeMode = _windowResizeMode;//恢复窗口可调整信息 
            IsFullscreen = false;
        }
    }
}
